package com.github.dice.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.dice.constant.ErrorEnums;
import com.github.dice.dto.ResultDTO;

@ControllerAdvice
public class GlobalExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ResultDTO defaultErrorHandler(Exception e) throws Exception {
        logger.warn("GlobalExceptionHandler.defaultErrorHandler is exception",e);
        return new ResultDTO<>(new BusinessException(ErrorEnums.E_9999));
    }

    @ExceptionHandler(value = BusinessException.class)
    @ResponseBody
    public ResultDTO defaultBusinessErrorHandler(BusinessException businessException) throws Exception {
        return new ResultDTO<>(businessException);
    }

}
